package com.our.sys.project1.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.web.UserSession;
import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.RunConstants;
import com.our.sys.project1.entity.ReadySend;
import com.our.sys.project1.service.DiscountPriceService;
import com.our.sys.project1.service.ReadyCodeService;

@Controller
@RequestMapping("/discount_price")
public class DiscountPriceController {

    @Autowired
    private ReadyCodeService readyCodeService;
    @Autowired
    private DiscountPriceService discountPriceService;
    
    private static Logger logger = LoggerFactory.getLogger(DiscountPriceController.class);
    
    @RequestMapping(value = "/query_send")
    public @ResponseBody
    Map<String, Object> queryReadySend(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<ReadySend> page = new Page<ReadySend>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("readySendTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        PropertyFilter filter = new PropertyFilter("NES_state", RunConstants.READY_DISCOUNT);
        filters.add(filter);

        Page<ReadySend> data = readyCodeService.querySend(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }
    
    @RequestMapping(value = "/save_discount")
    public @ResponseBody
    Map<String, Object> saveDiscount(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String readySendId = request.getParameter("readySendId");
        String strs = request.getParameter("strs");
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String allMoney = request.getParameter("allMoney");
        
        try {
            discountPriceService.saveDiscount(Long.valueOf(readySendId), strs, u.getAccount(), new BigDecimal(allMoney));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    
    @RequestMapping(value = "/delete_send")
    public @ResponseBody
    Map<String, Object> deleteSend(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String readySendId = request.getParameter("readySendId");
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        
        try {
            discountPriceService.deleteSend(Long.valueOf(readySendId), u.getAccount());
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
